<template>
  <div class="list">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <div
        v-for="(item, index) in list"
        :key="index"
        :title="item"
        class="song"
      >
        <h3>{{ item.name }}</h3>
        <div class="singers">
          <span v-for="(singer, index) in item.singer" :key="singer.id">
            {{ singer.name }}
            <span v-show="index != item.singer.length - 1">/</span>
            <!-- v-show判断,当index不是最后一个的时候，显示/分隔符。 -->
          </span>
          <span v-show="item.album">
            <span>.</span>
            <span>{{ item.album.name }}</span>
          </span>
          <span v-show="item.subtitle">
            <span>.</span>
            <span>{{ item.album.subtitle }}</span>
          </span>
          <!-- 当专辑名称不为空的时候添加·和专辑名称。  -->
        </div>
      </div>
    </van-list>
  </div>
</template>
<script>
export default {
  props: ["singeid"],
  data() {
    return {
      loading: false,
      finished: false,
      songnum: 30,
      songlist: [], //歌曲列表
      list: [], //vant的list列表数据
    };
  },
  created() {
    this.getData(this.singeid, 1, 10);
  },
  methods: {
    getData(id, page, num) {
      this.$request(
        "get",
        "/singer/songs?singermid=" + id + "&page=" + page + "&num" + num
      ).then((res) => {
        console.log(res);
        this.songlist = res.data.list;
      });
    },
    onLoad() {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      setTimeout(() => {
        this.list = this.list.concat(this.songlist.splice(0, 10));

        console.log(this.list);
        // 加载状态结束
        this.loading = false;

        // 数据全部加载完成
        if (this.list.length >= this.songnum) {
          this.finished = true;
        }
      }, 1000);
    },
  },
};
</script>
<style>
.song {
  margin-bottom: 1rem;
}
.song > h3 {
  text-align: left;
  font-size: 1rem;
  padding: 0 2rem;
  margin: 0;
}
.singers {
  text-align: left;
  padding: 0 2rem;
}
.singers span {
  font-size: 0.8rem;
  color: gray;
}
.singers > span:nth-last-child(1) > span:nth-child(1) {
  margin: 0 0.3rem;
  font-weight: 900;
  color: red;
}
.singers > span:nth-last-child(2) > span:nth-child(1) {
  margin: 0 0.3rem;
  font-weight: 900;
  color: red;
}
</style>
